
package com.soft.sitegenerator.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.soft.sitegenerator.business.hibernate.Person;
import com.soft.sitegenerator.exceptions.ObjectNotFoundException;
import com.soft.sitegenerator.form.PersonEditForm;

public class PersonEditAction extends DispatchAction {

	public ActionForward editPerson(
		ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response) {
		
		/*
		PersonListForm personListForm = (PersonListForm) form;
		Component component=new DataBaseComponent();
	    Person person = (Person) component.getBean("personsHib");*/
		
		
		
	    PersonEditForm personEditForm = (PersonEditForm) form;
		Long id = new Long(Long.parseLong(request.getParameter("id")));
	    Person person = (Person)personEditForm.getPerson();
	    person.setId(id);

	    try {
			person.load();
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
		}
	    
		return mapping.findForward("showEdit");
	}
	
	public ActionForward deletePerson(
		ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response) {
		PersonEditForm personEditForm = (PersonEditForm) form;

	    Long id = new Long(Long.parseLong(request.getParameter("id")));
		
	    personEditForm.getPerson().setId(id);
	    personEditForm.getPerson().delete();
		
		return mapping.findForward("showList");
	}
	
	public ActionForward addPerson(
		ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response) {
		
		return mapping.findForward("showAdd");
	}
	
	public ActionForward savePerson(
			ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response) {
		PersonEditForm personEditForm = (PersonEditForm) form;
		
		Person person = personEditForm.getPerson();
	    person.save();
		
		return mapping.findForward("showList");
		
	}

	public ActionForward updatePerson(
			ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response) {
			PersonEditForm personEditForm = (PersonEditForm) form;
			
			Long id = new Long(Long.parseLong(request.getParameter("id")));
			Person person = personEditForm.getPerson();
			person.setId(id);
			person.update();
			
			return mapping.findForward("showList");
			
		}

}